package com.yidu.qx.dao;

import com.yidu.qx.domain.QxUserEntity;
import com.yidu.qx.utils.Pages;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.util.StringUtils;

import java.util.Iterator;
import java.util.List;

/**
 * @program: jurisdiction
 * @description: 用户表实现类
 * @author: 张超
 * @create: 2019-09-25 14:07
 */
public class UserDaoImpl implements UserDao{
    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
    @Override
    public void add(QxUserEntity qxUserEntity) {
        hibernateTemplate.save(qxUserEntity);
    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void update(QxUserEntity qxUserEntity) {
        hibernateTemplate.update(qxUserEntity);
    }

    @Override
    public List<QxUserEntity> findAll(QxUserEntity qxUserEntity, Pages pages) {
        DetachedCriteria criteria=DetachedCriteria.forClass(QxUserEntity.class);
        if(!StringUtils.isEmpty(qxUserEntity.getUserName())){
            criteria.add(Restrictions.ilike("userName","%"+qxUserEntity.getUserName()+"%"));
        }
        List<QxUserEntity> list  = (List<QxUserEntity>) hibernateTemplate.findByCriteria(criteria,pages.getStartRows(),pages.getLimit());
        return list;
    }

    @Override
    public QxUserEntity findById(String id) {
        return hibernateTemplate.get(QxUserEntity.class,id);
    }
    @Override
    public  List<QxUserEntity> findName(String userName) {
        DetachedCriteria criteria=DetachedCriteria.forClass(QxUserEntity.class);
        criteria.add(Restrictions.eq("userName",userName));
        List<QxUserEntity> qxUserEntitylist= ( List<QxUserEntity>) hibernateTemplate.findByCriteria(criteria);
        return qxUserEntitylist;
    }
    @Override
    public Long count(QxUserEntity qxUserEntity) {
        DetachedCriteria criteria=DetachedCriteria.forClass(QxUserEntity.class);
        criteria.setProjection(Projections.rowCount());
        if(!StringUtils.isEmpty(qxUserEntity.getUserName())){
            criteria.add(Restrictions.ilike("userName","%"+qxUserEntity.getUserName()+"%"));
        }
        Long count = (Long)hibernateTemplate.findByCriteria(criteria).get(0);
        //Long count = (Long)hibernateTemplate.find("select count(*) from Emp").get(0);
        return count;
    }

    @Override
    public QxUserEntity findByIdThree(String id) {
        return hibernateTemplate.get(QxUserEntity.class,id);
    }

    //根据用户名和密码查询
    @Override
    public QxUserEntity find(String userName, String userPassword) {
        System.err.println("=================================================账号"+userName+"密码:"+userPassword);

        DetachedCriteria criteria = DetachedCriteria.forClass(QxUserEntity.class);
        criteria.add(Restrictions.eq("userName", userName));
        criteria.add(Restrictions.eq("userPassword", userPassword));
        criteria.add(Restrictions.eq("userState", "1"));
        List<QxUserEntity> list = (List<QxUserEntity>) hibernateTemplate.findByCriteria(criteria);
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            QxUserEntity entity = (QxUserEntity) iterator.next();
            System.out.println("1111111111111111111111111");
            return entity;
        }
        return null;
    }
}